<template>
  <div class="cinema-management-container">
    <!-- 影院列表页面 -->
    <div v-if="currentView === 'list'">
      <div class="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>影院管理</el-breadcrumb-item>
          <el-breadcrumb-item>影院列表</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <el-card class="cinema-list-card">
        <div class="list-header">
          <el-button type="primary" @click="showAddCinemaDialog">
            <el-icon><Plus /></el-icon> 添加影院
          </el-button>
          <el-input
              v-model="searchKeyword"
              placeholder="搜索影院名称或地址"
              class="search-input"
              clearable
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
        </div>

        <el-table :data="filteredCinemas" stripe style="width: 100%">
          <el-table-column prop="name" label="影院名称" width="200" />
          <el-table-column prop="address" label="地址" />
          <el-table-column prop="phone" label="电话" width="150" />
          <el-table-column prop="hallCount" label="影厅数量" width="120" align="center">
            <template #default="scope">
              <el-tag>{{ scope.row.hallCount }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180" align="center">
            <template #default="scope">
              <el-button size="small" @click="editCinema(scope.row)">编辑</el-button>
              <el-button size="small" type="danger" @click="deleteCinema(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <!-- 影院编辑页面 -->
    <div v-else-if="currentView === 'edit'">
      <div class="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>影院管理</el-breadcrumb-item>
          <el-breadcrumb-item @click="backToList">影院列表</el-breadcrumb-item>
          <el-breadcrumb-item>编辑影院</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <el-card class="cinema-edit-card">
        <el-form :model="currentCinema" label-width="150px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="影院名称" prop="name">
                <el-input v-model="currentCinema.name" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="影院电话" prop="phone">
                <el-input v-model="currentCinema.phone" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="影院地址" prop="address">
            <el-input v-model="currentCinema.address" />
          </el-form-item>

          <el-row>
            <el-col :span="12">
              <el-form-item label="开始营业时间" prop="workStartTime">
                <el-time-picker
                    v-model="currentCinema.workStartTime"
                    value-format="HH:mm"
                    placeholder="选择开始时间"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="结束营业时间" prop="workEndTime">
                <el-time-picker
                    v-model="currentCinema.workEndTime"
                    value-format="HH:mm"
                    placeholder="选择结束时间"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="拥有影厅类型">
            <div class="hall-management">
              <el-input
                  v-model="inputHall"
                  placeholder="输入影厅类型"
                  class="el-input-hall"
                  @keyup.enter.native="addHallCategory"
              />
              <el-button type="primary" @click="addHallCategory">
                <el-icon><Plus /></el-icon> 添加
              </el-button>

              <div class="hall-tags-edit">
                <el-tag
                    v-for="(item, index) in currentCinema.halls"
                    :key="index"
                    closable
                    @close="deleteHallCategory(index)"
                >
                  {{ item }}
                </el-tag>
              </div>
            </div>
          </el-form-item>

          <el-form-item label="影院图片">
            <el-upload
                action=""
                list-type="picture-card"
                :auto-upload="false"
                :file-list="currentCinema.pics"
                :on-change="handleChange"
                :on-remove="handleRemove"
            >
              <el-icon><Plus /></el-icon>
            </el-upload>

            <el-dialog v-model="imagePreviewDialog" append-to-body>
              <img width="100%" :src="dialogImageUrl" alt="影院图片预览">
            </el-dialog>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="saveCinema">保存</el-button>
            <el-button @click="backToList">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>

    <!-- 添加影院对话框 -->
    <el-dialog
        v-model="addDialogVisible"
        title="添加新影院"
        width="600px"
        @closed="resetAddForm"
    >
      <el-form :model="newCinema" label-width="100px">
        <el-form-item label="影院名称" required>
          <el-input v-model="newCinema.name" placeholder="请输入影院名称" />
        </el-form-item>
        <el-form-item label="影院地址" required>
          <el-input v-model="newCinema.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="联系电话" required>
          <el-input v-model="newCinema.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="营业时间">
          <el-time-picker
              is-range
              v-model="newCinema.businessHours"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              placeholder="选择营业时间"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addNewCinema">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'

export default {
  setup() {
    // 视图控制
    const currentView = ref('list') // 'list' 或 'edit'
    const currentCinema = ref(null)

    // 影院列表数据
    const cinemas = ref([
      {
        id: 1,
        name: '吾悦国际影城(朝阳店)',
        address: '北京市朝阳区建国路88号',
        phone: '010-88889999',
        hallCount: 8,
        workStartTime: '09:00',
        workEndTime: '23:00',
        halls: ['IMAX厅', '4D厅', '杜比全景声厅'],
        pics: [
          { url: 'https://via.placeholder.com/300x200?text=影院外观' },
          { url: 'https://via.placeholder.com/300x200?text=大厅' }
        ]
      },
      {
        id: 2,
        name: '吾悦国际影城(海淀店)',
        address: '北京市海淀区中关村大街99号',
        phone: '010-66667777',
        hallCount: 6,
        workStartTime: '10:00',
        workEndTime: '22:30',
        halls: ['VIP厅', '巨幕厅'],
        pics: [
          { url: 'https://via.placeholder.com/300x200?text=影院外观' }
        ]
      }
    ])

    // 搜索功能
    const searchKeyword = ref('')
    const filteredCinemas = computed(() => {
      if (!searchKeyword.value) return cinemas.value
      return cinemas.value.filter(cinema =>
          cinema.name.includes(searchKeyword.value) ||
          cinema.address.includes(searchKeyword.value)
      )
    })

    // 添加影院功能
    const addDialogVisible = ref(false)
    const newCinema = ref({
      name: '',
      address: '',
      phone: '',
      businessHours: [new Date(2023, 0, 1, 9, 0), new Date(2023, 0, 1, 23, 0)]
    })

    // 图片管理
    const imagePreviewDialog = ref(false)
    const dialogImageUrl = ref('')
    const inputHall = ref('')

    // 方法
    const showAddCinemaDialog = () => {
      addDialogVisible.value = true
    }

    const resetAddForm = () => {
      newCinema.value = {
        name: '',
        address: '',
        phone: '',
        businessHours: [new Date(2023, 0, 1, 9, 0), new Date(2023, 0, 1, 23, 0)]
      }
    }

    const addNewCinema = () => {
      const newId = Math.max(...cinemas.value.map(c => c.id), 0) + 1
      cinemas.value.push({
        id: newId,
        name: newCinema.value.name,
        address: newCinema.value.address,
        phone: newCinema.value.phone,
        workStartTime: newCinema.value.businessHours[0].toTimeString().substring(0, 5),
        workEndTime: newCinema.value.businessHours[1].toTimeString().substring(0, 5),
        hallCount: 0,
        halls: [],
        pics: []
      })
      addDialogVisible.value = false
      ElMessage.success('影院添加成功')
    }

    const editCinema = (cinema) => {
      currentCinema.value = { ...cinema }
      currentView.value = 'edit'
    }

    const backToList = () => {
      currentView.value = 'list'
    }

    const saveCinema = () => {
      const index = cinemas.value.findIndex(c => c.id === currentCinema.value.id)
      if (index !== -1) {
        cinemas.value[index] = currentCinema.value
      }
      backToList()
      ElMessage.success('影院信息更新成功')
    }

    const deleteCinema = (id) => {
      cinemas.value = cinemas.value.filter(cinema => cinema.id !== id)
      ElMessage.success('影院删除成功')
    }

    const addHallCategory = () => {
      if (inputHall.value && !currentCinema.value.halls.includes(inputHall.value)) {
        currentCinema.value.halls.push(inputHall.value)
        inputHall.value = ''
      }
    }

    const deleteHallCategory = (index) => {
      currentCinema.value.halls.splice(index, 1)
    }

    const handleRemove = (file) => {
      const index = currentCinema.value.pics.findIndex(p => p.url === file.url)
      if (index !== -1) {
        currentCinema.value.pics.splice(index, 1)
      }
    }

    const handleChange = (file) => {
      if (!currentCinema.value.pics.some(p => p.url === file.url)) {
        currentCinema.value.pics.push({
          url: file.url,
          name: file.name
        })
      }
    }

    const handlePictureCardPreview = (file) => {
      dialogImageUrl.value = file.url
      imagePreviewDialog.value = true
    }

    return {
      currentView,
      currentCinema,
      cinemas,
      searchKeyword,
      filteredCinemas,
      addDialogVisible,
      newCinema,
      imagePreviewDialog,
      dialogImageUrl,
      inputHall,
      showAddCinemaDialog,
      resetAddForm,
      addNewCinema,
      editCinema,
      backToList,
      saveCinema,
      deleteCinema,
      addHallCategory,
      deleteHallCategory,
      handleRemove,
      handleChange,
      handlePictureCardPreview
    }
  }
}
</script>

<style scoped>
.cinema-management-container {
  padding: 20px;
}

.header {
  margin-bottom: 20px;
}

.cinema-list-card, .cinema-edit-card {
  margin-top: 20px;
}

.list-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.search-input {
  width: 300px;
}

.el-table {
  margin-top: 15px;
}

.el-input-hall {
  width: 240px;
  margin-right: 15px;
}

.hall-management {
  margin-bottom: 15px;
}

.hall-tags-edit {
  margin-top: 15px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-row {
  margin-bottom: 15px;
}
</style>
